package com.ninetaildemonfox.zdl.socialproject.mvp.ui.activity;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.kongzue.dialog.v3.WaitDialog;
import com.ninetaildemonfox.zdl.socialproject.base.BaseActivity;
import com.ninetaildemonfox.zdl.socialproject.R;
import com.ninetaildemonfox.zdl.socialproject.mvp.contract.CommonPreoblemContract;
import com.ninetaildemonfox.zdl.socialproject.mvp.presenter.CommonProblemPresenter;
import com.ninetaildemonfox.zdl.socialproject.mvp.ui.adapter.CommonProblemAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author NineTailDemonFox
 * @date 2019/7/23 10:01
 * 功能描述： 常见问题
 * 联系方式：1037438704@qq.com
 */

public class CommonProblemActivity extends BaseActivity implements CommonPreoblemContract, BaseQuickAdapter.OnItemClickListener {
    @BindView(R.id.image_left_finish)
    ImageView imageleftFinish;
    @BindView(R.id.text_center)
    TextView textCenter;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    private CommonProblemPresenter common;
    private CommonProblemAdapter commonProblemAdapter;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    private String problemInfo;

    @Override
    protected int getLayoutResource() {
        return R.layout.activity_common_problem;
    }

    @Override
    protected void onInitialization(Bundle bundle) {
        textCenter.setText("常见问题");
        common = new CommonProblemPresenter(this, me);
        imageleftFinish.setVisibility(View.VISIBLE);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        commonProblemAdapter = new CommonProblemAdapter(R.layout.list_common_problem_adp);
        recyclerView.setAdapter(commonProblemAdapter);
        //数据请求
        common.getProblemList(token, pageSize);
        commonProblemAdapter.setOnItemClickListener(this);
        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                pageSize++;
                common.getProblemList(token, pageSize);
            }
        });

        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                pageSize = 1;
                common.getProblemList(token, pageSize);
            }
        });
    }

    @OnClick({R.id.image_left_finish})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.image_left_finish:
                finish();
                break;
            default:
        }
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        Map<String, String> map = commonProblemAdapter.getData().get(position);
        common.getProblemInfo(token,map.get("id"));
    }

    @Override
    public void Success(List<Map<String, String>> data) {
        refreshLayout.finishRefresh();
        refreshLayout.finishLoadMore();
        if (pageSize == 1) {
            commonProblemAdapter.setNewData(data);
        } else {
            commonProblemAdapter.addData(data);
        }
    }

    @Override
    public void Failed(String msg) {

    }

    @Override
    public void showLoading() {
        WaitDialog.show(me, "正在登录");
    }
}